package top.igotcha.mapreduce.flowcount.reducer;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import top.igotcha.mapreduce.flowcount.bean.FlowBean;

import java.io.IOException;

/**
 * @time 2021/1/10
 * @auth Gotcha
 * @describe
 */
public class FlowCountReducer extends Reducer<Text, FlowBean,Text, FlowBean> {
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {
        //封装新的flowBean
        FlowBean flowBean = new FlowBean();
        Integer upFlow = 0 ;
        Integer downFlow = 0 ;
        Integer upCountFlow = 0 ;
        Integer downCountFlow = 0 ;
        for (FlowBean value : values) {
            upFlow+=value.getUpFlow();
            downFlow+=value.getDownFlow();
            upCountFlow+=value.getUpCountFlow();
            downCountFlow+=value.getDownCountFlow();
        }
        flowBean.setUpFlow(upFlow);
        flowBean.setDownFlow(downFlow);
        flowBean.setUpCountFlow(upCountFlow);
        flowBean.setDownCountFlow(downCountFlow);
        //将k3,v3写入上下文
        context.write(key,flowBean);
    }
}
